package com.fjc.cloud.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjc.cloud.order.entity.Order;
import com.fjc.cloud.order.feign.UserFeignClient;
import com.fjc.cloud.order.mapper.OrderMapper;
import com.fjc.cloud.order.service.OrderService;
import com.fjc.cloud.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService{
    @Autowired
    private OrderMapper orderMapper;
    //替代RestTemplate 进行远程方法调用
    @Autowired
    private UserFeignClient userFeignClient;

    //新需求：查询订单信息的时候，把对应用户信息查询出来
    @Override
    public Order findOrderByOrderId(Long orderId) {
        //步骤1：根据订单id查询订单信息
        Order order = orderMapper.selectOrderByOrderId(orderId);
        //步骤2：发起远程调用，查询用户信息
        //@LoadBalanced  //负载均衡 启用后，可以在服务调用时直接使用服务名：
        //User user = restTemplate.getForObject("http://my-cloud-user/api/user/findUserById/" + order.getUserId(), User.class);
        User user = userFeignClient.findUserById(order.getUserId());
        //步骤3：将用户信息设置到订单对象中
        order.setUser(user);
        //步骤4：返回订单信息
        return order;
    }

}

